import maya.cmds as mc
import re

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#This Function can getParamAtPoint
#Note: for pointOnCurveInfo node's param (Very thanks for ZhaoChunHai and ZhangJian !!! )
#FN: getParam(crv , obj)
#Date: 2012/10/30_v1.0

import maya.cmds as mc
import maya.OpenMaya as om

def getSpace(obj):
        '''get one object's worldSpace value'''
        objT = mc.xform(obj,q=1,t=1,ws=1)
        objR = mc.xform(obj,q=1,ro=1,ws=1)
        aboutObjDate = objT + objR
        return aboutObjDate


def node_by_name(name, root = om.MObject.kNullObj):
        dagiter = om.MItDag(om.MItDag.kDepthFirst)
        if not root is om.MObject.kNullObj:
            dagiter.reset(root)
        while not dagiter.isDone():
            pf = om.MFnDagNode(dagiter.currentItem())
            if pf.name() == name:
                return dagiter.currentItem()
            dagiter.next()
        return om.MObject.kNullObj

def getParam(crv , obj):

        curveshape = mc.listRelatives(crv,s=True)
        objspace = getSpace(obj)

        curveObj = node_by_name(curveshape[0])
        curveFn = om.MFnNurbsCurve(curveObj)
        mpoint = om.MPoint( objspace[0],objspace[1],objspace[2],1.0)
        nearestPoint = curveFn.closestPoint(mpoint,None,0.0001,om.MSpace.kTransform )
        u_util = om.MScriptUtil()
        uPtr = u_util.asDoublePtr()
        curveFn.getParamAtPoint(nearestPoint,uPtr,om.MSpace.kTransform)
        u1 = u_util.getDouble(uPtr)
        return u1
	
def fixedCtrlOnCurve():
	curvename=mc.textFieldButtonGrp('FixedCtrls',q=True, text=True)
	objsel=mc.ls(sl=True)
	shapecurve=mc.listRelatives(curvename,s=True)
	
	num=0
	for i in objsel:
		namePoci=str(i)+'_poci'
		mc.createNode('pointOnCurveInfo',n=namePoci)
		parameter=getParam(curvename , i)
		mc.connectAttr(str(shapecurve)+'.worldSpace',namePoci+'.inputCurve')
		mc.connectAttr(namePoci+'.result.position',str(i)+'.translate')
		mc.setAttr(namePoci+'.parameter',parameter)
		
		

